<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/common/taglib.jsp" %>    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script>
	//# sourceURL=orgPersonList.js
	
	var selOrgId = null;
	var selOrgName = null;

	$(document).ready(function() {

		// 选择表格行
		$("#gridOrganization table > tbody > tr").click(function() {
			$("#gridOrganization table > tbody > tr").removeClass('info');
			$(this).addClass('info');
			selOrgId = $(this)[0].id;
			selOrgName = $(this).find("td").eq(1).text();
		});

	});

	function addOrganization(orgType) {
		if (check_addOrganization(orgType) == false) return;
		
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/organization/add.action?parentId=${parentOrgId}&orgType=' + orgType
		});
	}

	function editOrganization() {
		if (checkSelected() == false) return;
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/organization/modify.action?orgId=' + selOrgId
		});		
	}

	function changeState(state) {
		if (checkSelected() == false) return;
		
		if(state == "Delete" && confirm('其下属组织，以及关联下属人员都将相应处理, 确定要删除"' + selOrgName + '"吗？') == false) {
			return;
		}
		
		if(state == "Invalid" && confirm('其下属组织，以及关联下属人员都将相应处理, 确定要禁用"' + selOrgName + '"吗？') == false) {
			return;
		}		
		
		var url = "${ctx}/org/organization/changeState.action?orgId=" + selOrgId + "&validState=" + state;
		$.get(url, function(data) {	    				    		
			if(data == "success") {
				refreshPage();
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});			
	}
	
	function orgTreeView() {
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/organization/treeView.action?orgId=0&isContainSelf=true&displayLevel=Invalid'
		});		
	}
	
	function onSelectOrgEvent(orgId) {
		$('#dialog_main').modal('hide');
		enterPage(orgId);
	}

	function checkSelected() {
		if (selOrgId == null) {
			alert("请选择表格行");
			return false;
		} else {
			return true;
		}
	}
	
	function check_addOrganization(orgType) {

		if (("${parentOrgId}" == 0)&&(orgType != 'ORG')) {
			alert("根节点下，只能新建[机构]");
			return false;
		} else if ("${parentOrgType}" == 'POS') {
			alert("岗位节点下，不允许新建子组织");
			return false;
		} else if (("${parentOrgType}" == 'DEP')&&(orgType == 'ORG')) {
			alert("部门节点下，不允许新建[机构]");
			return false;
		}

		return true;
	}

	function dialogSubmitSuccess() {
		$('#dialog_main').modal('hide');
		refreshPage();
	}

    function refreshPage() {
		var url = "/org/organization/manage.action?parentId=" + ${parentOrgId};
		load_main_content(url);
    }
    
    function enterPage(parentId) {
		var url = "/org/organization/manage.action?parentId=" + parentId
		load_main_content(url);
    }    

</script>

</head>
<body>

<div>
	<h3>组织管理</h3>
</div>

<nav class="navbar navbar-default" role="navigation">
  <ul class="nav navbar-nav navbar-left">
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">新建</a>
      <ul class="dropdown-menu">
        <li>
          <a href="javascript:addOrganization('ORG')">机构</a></li>
        <li>
          <a href="javascript:addOrganization('DEP')">部门</a></li>     
	    <li>
	      <a href="javascript:addOrganization('POS')">岗位</a></li>
      </ul>
    </li>
    <li>
      <a href="javascript:editOrganization()">编辑</a></li>
    <li>
      <a href="javascript:orgTreeView()">树图</a></li>      
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">更多
        <b class="caret"></b></a>
      <ul class="dropdown-menu">
        <li>
          <a href="javascript:changeState('Invalid')">禁用</a></li>
        <li>
          <a href="javascript:changeState('Normal')">启用</a></li>
        <li class="divider"></li>          
	    <li>
	      <a href="javascript:changeState('Delete')">删除</a></li>
      </ul>
    </li>
  </ul>
</nav>

<ol class="breadcrumb" style="background-color: white;">
	<li><a href="javascript:enterPage(0)">组织机构</a></li>
	<c:forEach items="${parentOrgPath}" var="node" varStatus="status">
		<c:choose>
			<c:when test="${status.last}"><li class="active">${node.orgName}</li></c:when>
			<c:otherwise><li><a href="javascript:enterPage(${node.orgId})">${node.orgName}</a></li></c:otherwise>
		</c:choose>
	</c:forEach>	
</ol>

<div id="gridOrganization" class="table-responsive">
  <table class="table table-hover table-bordered">
    <thead>
      <tr>
        <th>类型</th>          
        <th>名称</th>
        <th>编码</th>
        <th>状态</th>
        <th>操作</th></tr>
    </thead>
    <tbody>
		<c:forEach items="${listOrganization}" var="node" varStatus="status">		
			<tr id='${node.orgId}'>
			<td>${node.displayOrgType}</td>			
			<td>${node.orgName}</td>
			<td>${node.code}</td>
			<td>${node.displayValidState}</td>
			<c:choose>
				<c:when test="${node.orgType == 'POS'}"><td></td></c:when>
				<c:otherwise><td><a href="javascript:enterPage(${node.orgId})">进入</a></td></c:otherwise>
			</c:choose>			

			</tr>
		</c:forEach>
    </tbody>
  </table>
</div>

</body>
</html>